package com.zhenyi.youqu.admin.index;

import java.util.List;

import com.baijob.commonTools.DateUtil;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.utils.DateUtils;
import com.youqu.common.model.YqItem;
import com.youqu.common.model.YqProduct;
import com.youqu.common.model.YqShopUser;
import com.zhenyi.youqu.core.YQConfig;

public class ItemController extends YQAdminController {

	@Override
	public void list() {
		Kv kv = getSimpleKv("keyword");
		SqlPara sqlPara = db.getSqlPara("YQItems", kv);
		Page<Record> pages = db.paginate(getPage(), getPageSize(), sqlPara);
		setRainbow(pages);
		setData("items", pages.getList());
		render("/yq/admin/item/list.html");
	}

	@Override
	public void info() {
		setReferer();
		Long itemId = getParaToLong("itemId");
		YqShopUser user = getShopUser();

		YqItem yqItem;// = YqItem.dao.findById(itemId);
		if (itemId == null) {
			yqItem = new YqItem();
			yqItem.setType(1);
		} else {
			yqItem = YqItem.dao.findById(itemId);
			if (yqItem == null) {
				renderError(404);
				return;
			}
		}

		SqlPara sqlPara = db.getSqlPara("YQProducts", Kv.by("appId", user.getAppId()));
		List<YqProduct> products = YqProduct.dao.find(sqlPara);
		setData("products", products);
		setInfoItem(yqItem);
		// setData(yqItem);
		setAttr("data", JsonKit.toJson(yqItem));
		render("/yq/admin/item/info.html");
	}

	@Override
	public void submit() {
		String title = getPara("title");
		String subtitle = getPara("subtitle");
		String cover = getPara("cover");
		String intro = getPara("intro");
		Integer type = getParaToInt("type");
		Long productId = getParaToLong("productId");
		String url = getPara("url");
		String images = getPara("images");
		String longitude = getPara("longitude");
		String latitude = getPara("latitude");
		Integer isEnable = getParaToInt("isEnable", 1);
		Integer isHot = getParaToInt("isHot", 0);
		Integer isTop = getParaToInt("isTop", 0);
		String address = getPara("address");
		String famous = getPara("famous");
		String startTime = getPara("startTime");
		String endTime = getPara("endTime");
		Long itemId = getParaToLong("itemId");
		Integer isHidden = getParaToInt("isHidden", 0);
		Integer subscribeCount = getParaToInt("subscribeCount");
		String tags = getPara("tags");
		String shareTitle = getPara("shareTitle");
		String shareImage = getPara("shareImage");

		String coverUrl = getPara("coverUrl");
		if (StrKit.isBlank(cover)) {
			cover = coverUrl;
		}
		YqItem yqItem;
		if (itemId == null) {
			yqItem = new YqItem();
			yqItem.save();
		} else {
			yqItem = YqItem.dao.findById(itemId);
		}
		yqItem.setTitle(title);
		yqItem.setSubtitle(subtitle);
		yqItem.setCover(cover);
		yqItem.setIntro(intro);
		yqItem.setType(type);
		yqItem.setProductId(productId);
		yqItem.setUrl(url);
		yqItem.setImages(images);
		yqItem.setLatitude(latitude);
		yqItem.setLongitude(longitude);
		yqItem.setAddress(address);
		yqItem.setIsEnable(isEnable);
		yqItem.setFamous(famous);
		yqItem.setIsHot(isHot);
		yqItem.setIsTop(isTop);
		yqItem.setIsHidden(isHidden);
		if (subscribeCount != null) {
			yqItem.setSubscribeCount(subscribeCount);
		}
		yqItem.setTags(tags);
		yqItem.setShareTitle(shareTitle);
		yqItem.setShareImage(shareImage);
		yqItem.setStartTime(StrKit.notBlank(startTime) ? DateUtil.parse(startTime, DateUtils.YYYYMMDD_HHMMSS) : null);
		yqItem.setEndTime(StrKit.notBlank(endTime) ? DateUtil.parse(endTime, DateUtils.YYYYMMDD_HHMMSS) : null);
		yqItem.update();
		// yqItem.setStartTime(startTime);
		renderSuccess();

		// String key = APP_ID + "_itemsSize";
		// cache.setex(key, 7200, YqItem.dao.findAll().size());
		/**
		 * 更新新id
		 */
		if (isHidden == 1) {
			cache.srem(YQConfig.KEY_ITEM_INDEX, yqItem.getId());
		} else {
			cache.sadd(YQConfig.KEY_ITEM_INDEX, yqItem.getId());
		}
	}

	@Override
	public void delete() {
		Long itemId = getParaToLong("itemId");
		if (itemId == null) {
			renderError(404);
			return;
		}
		YqItem yqItem = YqItem.dao.findById(itemId);
		if (yqItem == null) {
			renderError(404);
			return;
		}
		yqItem.delete();
		cache.srem(YQConfig.KEY_ITEM_INDEX, yqItem.getId());
		renderSuccess();
	}

	public void updateIsTop() {
		Long itemId = getParaToLong("itemId");
		Integer isTop = getParaToInt("isTop", 1);
		if (itemId == null) {
			renderFailure(500, "item id");
			return;
		}
		YqItem item = YqItem.dao.findById(itemId);
		if (item == null) {
			renderError(404);
			return;
		}
		SqlPara sp = db.getSqlPara("YQItems");
		List<YqItem> list = YqItem.dao.find(sp);
		for (YqItem yqItem : list) {
			yqItem.setIsTop(0);
			yqItem.update();
		}
		item.setIsTop(isTop);
		renderFlag(item.update());
	}

	public void updateIsHidden() {
		Long itemId = getParaToLong("itemId");
		Integer isHidden = getParaToInt("isHidden", 1);
		if (itemId == null) {
			renderFailure(500, "item id");
			return;
		}
		YqItem item = YqItem.dao.findById(itemId);
		if (item == null) {
			renderError(404);
			return;
		}
		item.setIsHidden(isHidden);
		if (isHidden == 1) {
			cache.srem(YQConfig.KEY_ITEM_INDEX, itemId);
		} else {
			cache.sadd(YQConfig.KEY_ITEM_INDEX, itemId);
		}
		renderFlag(item.update());
	}
}
